<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
</head>
<body>
	<div class="easyui-layout" data-options="fit:true">
		<form id="frame_server_form">
			<div data-options="region:'center',border:false" style="padding:10px 20px;">
				<div id="server_main_div" class="infoTable">
					
				</div>
				<a href="#" onclick="submitServerInfo(this)" type class="easyui-linkbutton">提交</a>
			</div>
		</form>
	</div>
<script type="text/javascript">
function submitServerInfo() {
	var postData = {petServerList:[]};
	if(!$("#server_main_div>div :checkbox:checked").length) {
		msg.error("没有选服务");
		return;
	} 
	$("#server_main_div > div").each(function(){
		var $serverDiv = $(this);
		if($serverDiv.find(":checkbox").is(":checked")) {
			var petServer = {petServerValuationList:[], petServerRestrainList:[]};
			petServer.serverCode = $serverDiv.attr("serverCode");
			petServer.valueType = $serverDiv.attr("valueType");
			$serverDiv.children("div").each(function(){
				var $div = $(this);
				if($div.is("[restrain]")) {
					var restrain = {};
					restrain.sysId = $div.attr("restrain");
					restrain.restrainVal = $div.children("[name='restrainVal']").val();
					petServer.petServerRestrainList.push(restrain);
				} else {
					var valuation = {};
					valuation.sysId = $div.attr("valuation");
					valuation.valueVal = $div.children("[name='valueVal']").val();
					petServer.petServerValuationList.push(valuation);
				}
			});
			postData.petServerList.push(petServer);
		}
	});
	if($("#frame_server_form").form("validate")) {
		$.post("${ctx}/server/saveserver.do", tools.json2Data("host", postData), function(res){
			if(res.code == 1) {
				msg.msg("保存成功");
			} else {
				msg.error("保存出错");
			}
		}, "json");
	}	
}
$.getJSON("${ctx}/server/getsys.do", function(serList) {
	var $mainDiv = $("#server_main_div");
	$.each(serList, function(index, server) {
		var $serverDiv = $("<div></div>").addClass("infoItem").attr({valueType:server.valueType, serverCode:server.serverCode});
		//$serverDiv.append("<input type='checkbox' value=" + server.id + "/>");
		$serverDiv.append($("<span><input type='checkbox' value=" + server.id + "/>" + server.serverName + "</span>").addClass("infoName"));
		if(server.petServerValuationList != null) {
			$.each(server.petServerValuationList, function(){
				var $valuationDiv = $("<div></div>").attr("valuation", this.id);
				if(this.valueName != null && this.valueName != "") {
					$valuationDiv.append("<span>" + this.valueName + "</span>");
				}
				$valuationDiv.append("<input name='valueVal'/>");
				$serverDiv.append($valuationDiv);
			});
		}
		if(server.petServerRestrainList != null) {
			$.each(server.petServerRestrainList, function(){
				var $restrainDiv = $("<div></div>").attr("restrain", this.id);
				$restrainDiv.append("<span>" + this.restrainName + "</span>");
				$restrainDiv.append("<input name='restrainVal'/>");
				$serverDiv.append($restrainDiv);
			});
		}
		$mainDiv.append($serverDiv);
	});
	serverEditInit();
});
function serverEditInit() {
	$.getJSON("${ctx}/host/get.do", function(host){
		if($.isArray(host.petServerList)) {
			$.each(host.petServerList, function(index, petServer) {
				var $serverDiv = $("div[serverCode='" + this.serverCode + "']");
				if($serverDiv.length == 1) {
					$serverDiv.find(":checkbox").attr("checked", "checked");
					$.each(petServer.petServerValuationList, function() {
						$serverDiv.children("div[valuation='" + this.sysId + "']").children("input[name='valueVal']").val(this.valueVal);
					});
					if(petServer.petServerRestrainList != null) {
						$.each(petServer.petServerRestrainList, function() {
							$serverDiv.children("div[restrain='" + this.sysId + "']").children("input[name='restrainVal']").val(this.restrainVal);
						});
					}
				}
			});
		}
	});
}
</script>
	<script type="text/javascript" src="${ctx}/js/utils/prompt.js?v=${v}"></script>
</body>
</html>